package com.paradogs.gs.modules;

import com.paradogs.gs.misc.*;
import com.paradogs.gs.modules.HelloEntity.HelloEntityMgr;
import com.paradogs.gs.modules.attr.AttrMgr;
import com.paradogs.gs.modules.equip.EquipBaseMgr;
import com.paradogs.gs.modules.equip.EquipMgr;
import com.paradogs.gs.modules.friend.FriendApplyMgr;
import com.paradogs.gs.modules.friend.FriendMgr;
import com.paradogs.gs.modules.inventory.ConsumableMgr;
import com.paradogs.gs.modules.inventory.InventoryMgr;
import com.paradogs.gs.modules.inventory.MaterialMgr;
import com.paradogs.gs.modules.role.RoleMgr;
import com.paradogs.gs.modules.role.msg.KickData;
import com.paradogs.gs.modules.role.msg.KickType;
import com.paradogs.gs.modules.world.WorldMgr;
import com.paradogs.gs.utils.PlayerUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: yumi
 * @date: 2024/10/15  18:41
 * @Description: 玩家
 */
@Slf4j
@Getter
@Accessors(fluent = true)
public class Player extends GameObject {

    @Setter
    private boolean online = false;

    // base
    private _NetMgr _netMgr;
    private InventoryMgr inventoryMgr;
    // world
    private WorldMgr worldMgr;

    // data
    private AttrMgr attrMgr;
    private RoleMgr roleMgr;
    private MaterialMgr materialMgr;
    private ConsumableMgr consumableMgr;
    private FriendMgr friendMgr;
    private FriendApplyMgr friendApplyMgr;
    private EquipBaseMgr equipBaseMgr;
    private EquipMgr equipMgr;
    private HelloEntityMgr helloEntityMgr;

    public Player(Long id) {
        super(id);
    }

    @Override
    public void StartBefore() {
        super.StartBefore();
        getMgrs().forEach(IManager::StartBefore);
    }

    @Override
    public void Start() {
        super.Start();
        getMgrs().forEach(mgr -> {
            if (IDataManager.class.isAssignableFrom(mgr.getClass())) {
                ((IDataManager<?, ?>) mgr).syncAll();
            }
        });
        SyncDataUpdater.autoSync();
        SyncDataUpdater.clear();
        getMgrs().forEach(IManager::Start);
    }

    @Override
    public void Destroy() {
        super.Destroy();
        getMgrs().forEach(IManager::Destroy);
    }

    @Override
    public void Update() {
        super.Update();
        getMgrs().forEach(IManager::Update);
    }

    public void save2DB() {
        getMgrs().forEach(mgr -> {
            if (IDataManager.class.isAssignableFrom(mgr.getClass())) {
                ((IDataManager<?, ?>) mgr).save2DB();
            }
        });
    }

    public void kick() {
        Destroy();
        _netMgr.kick(new KickData(KickType.REPEAT_LOGIN));
        PlayerSession session = PlayerUtils.delSession(getId());
        if (session != null && session.getPlayer() != null) {
            session.getPlayer().online(false);
        }
    }

}
